/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';

import { initByFAJsEmptyApp, initExecuteCommand, runHvigor } from '../../common/common';
import { HOS_SIGNATURE_TEMPLATE, Tasks } from '../constants';
import { buildTest, checkSuccessful, configJsProject } from '../utils';

describe('Hos Api8 js mixed devices test', () => {
  test.concurrent('Test build hap package with signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        undefined,
        [{
          moduleName: 'entry',
          deviceTypes: ['liteWearable', 'wearable'],
        }],
        { sdkVersion: 8, signConfig: HOS_SIGNATURE_TEMPLATE });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { mixed: true });
    }, { sign: true });
  });

  test.concurrent('Test build hap package without signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        undefined,
        [{
          moduleName: 'entry',
          deviceTypes: ['liteWearable', 'wearable'],
        }],
        { sdkVersion: 8 });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { mixed: true, signed: false });
    }, { sign: false });
  });

  test.concurrent('Test build app package with signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'project',
        product: 'default',
        tasks: Tasks.ASSEMBLE_APP,
      };

      configJsProject(projectRoot,
        undefined,
        [{
          moduleName: 'entry',
          deviceTypes: ['liteWearable', 'wearable'],
        }],
        { sdkVersion: 8, signConfig: HOS_SIGNATURE_TEMPLATE });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_APP, { mixed: true });
    });
  });

  test.concurrent('Test build app package without signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'project',
        product: 'default',
        tasks: Tasks.ASSEMBLE_APP,
      };


      configJsProject(projectRoot,
        undefined,
        [{
          moduleName: 'entry',
          deviceTypes: ['liteWearable', 'wearable'],
        }],
        { sdkVersion: 8 });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_APP, { mixed: true, signed: false });
    }, { sign: false });
  });

  test.concurrent('Test build ohos project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        [{
          moduleName: 'entry',
          updateTarget: [{ name: 'default', runtimeOS: 'OpenHarmony' }],
        }],
        [{ moduleName: 'entry', deviceTypes: ['liteWearable', 'wearable'] }],
        { sdkVersion: 8, updateProducts: [{ name: 'default', bundleName: 'com.example.myapplication' }] });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { mixed: true });
    });
  });

  test.concurrent('Test build hap package that add phone to devices', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot, undefined,
        [{
          moduleName: 'entry',
          deviceTypes: ['liteWearable', 'phone'],
        }],
        { sdkVersion: 8, signConfig: HOS_SIGNATURE_TEMPLATE });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { mixed: true });
    });
  });

  test.concurrent('Test build api9 project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: 'default@LegacyPreBuild',
      });

      configJsProject(projectRoot,
        undefined,
        [{
          moduleName: 'entry',
          deviceTypes: ['liteWearable', 'wearable'],
        }],
        { sdkVersion: 9 });

      const result = runHvigor(projectRoot, commands);
      expect(result.stderr.includes('Packaging API9 & Lite-Device project is not supported.')).toBeTruthy();
    });
  });

});
